#ifndef HIDDEVICE_H
#define HIDDEVICE_H

#include <hidapi.h>
#include <QString>
#include <QList>
#include <QObject>

class HIDDevice : public QObject
{
    Q_OBJECT

public:
    // 设备信息结构体
    struct DeviceInfo {
        unsigned short vendorId;
        unsigned short productId;
        QString manufacturerString;
        QString productString;
        QString serialNumber;
        QString path;
        int interfaceNumber;
        int releaseNumber;
    };

    explicit HIDDevice(QObject *parent = nullptr);
    ~HIDDevice();

    // 初始化HID系统
    static bool initialize();
    // 关闭HID系统
    static void exit();
    
    // 枚举设备
    static QList<DeviceInfo> enumerateDevices(unsigned short vendorId = 0, unsigned short productId = 0);
    
    // 打开设备
    bool open(const QString& path);
    bool open(unsigned short vendorId, unsigned short productId);
    
    // 关闭设备
    void close();
    
    // 数据传输
    bool write(unsigned char *data, size_t data_length);
     bool write1(unsigned char *data, size_t data_length);
    bool read(unsigned char *data,size_t data_length, const int milliseconds=1000);
    bool sendFeatureReport(const QByteArray& data);
    bool getFeatureReport(QByteArray& data, size_t reportId = 0);
    
    // 设备状态
    bool isConnected() const { return device != nullptr && isOpen; }
    QString getLastError() const;
    
    // 设备信息获取
    QString getManufacturerString() const;
    QString getProductString() const;
    QString getSerialNumberString() const;
hid_device* device;
signals:
    void dataReceived(const QByteArray& data);
    void errorOccurred(const QString& error);
    void deviceConnected();
    void deviceDisconnected();

private:

    bool isOpen;
    static const int BUFFER_SIZE = 65; // 通常HID报告的最大大小
};

#endif // HIDDEVICE_H 
